import { defineStore} from 'pinia'
import { aiAnswer } from '@/api/ai';
import {Message , APIResponse} from '@/api/ai/types'

interface State {
    messages : Message[]
  }
  
export const AiStore = defineStore('ai', {
    //属性
    state: (): State => {
        return {
           messages:[]   
        }
      },
    //数据处理
    getters: {
        
      
    },
    //函数
    actions: {
        async getAiAnswer(){
      try{
         let  res:APIResponse = await aiAnswer(this.messages)
         if(res.status_code === 200){
            this.messages.push(res.output.choices[0].message)
         }
         else{
          this.messages.push({
            content: "请求失败，请稍后再试！！！",
            role: "assistant"
        })
         }
          }
          catch(error){
            console.error(error)
          }
          
        }
        
    },
// 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        // 自定义名称
        key: 'ai',
        // 保存位置，默认保存在sessionStorage
        storage: localStorage,
        paths: ['messages'],
      },
    ],
  },

  })